"use strict";
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var BaseController_1 = require('./BaseController');
var AddDocDto_1 = require('../dtos/docs/AddDocDto');
var UpdateDocDto_1 = require('../dtos/docs/UpdateDocDto');
var DocsService_1 = require('../services/DocsService');
var GroupsService_1 = require('../services/GroupsService');
var ProjectsService_1 = require('../services/ProjectsService');
var DocsWithProjectDto_1 = require('../dtos/docs/return/DocsWithProjectDto');
var DocController = (function (_super) {
    __extends(DocController, _super);
    function DocController(app) {
        _super.call(this, app);
        var $this = this;
        app.post(this.domain + "/docs", function (req, res) {
            $this.handerErr(req, res, function (user) {
                $this.addDoc(req, res);
            }, true);
        });
        app.get(this.domain + "/docs/:id", function (req, res) {
            $this.handerErr(req, res, function () {
                $this.getDoc(req, res);
            }, true);
        });
        app.put(this.domain + "/docs/:id", function (req, res) {
            $this.handerErr(req, res, function () {
                $this.updateDoc(req, res);
            }, true);
        });
        app.delete(this.domain + "/docs/:id", function (req, res) {
            $this.handerErr(req, res, function () {
                $this.deleteDoc(req, res);
            }, true);
        });
        app.get(this.domain + "/docs/project/:id", function (req, res) {
            $this.handerErr(req, res, function () {
                $this.getDocByProjectId(req, res);
            }, true);
        });
        app.get(this.domain + "/docs/group/:id", function (req, res) {
            $this.handerErr(req, res, function () {
                $this.getDocByGroupId(req, res);
            }, true);
        });
        this.docsService = new DocsService_1.DocsService();
        this.groupsService = new GroupsService_1.GroupsService();
        this.projectsService = new ProjectsService_1.ProjectsService();
    }
    DocController.prototype.addDoc = function (req, res) {
        this.docsService.addDoc(AddDocDto_1.AddDocDto.toDoc(req.body), function (err, doc) {
            res.send(JSON.stringify(doc));
        });
    };
    DocController.prototype.getDoc = function (req, res) {
        var docId = req.params.id;
        this.docsService.getDoc(docId, function (err, doc) {
            res.send(JSON.stringify(doc));
        });
    };
    DocController.prototype.getDocByGroupId = function (req, res) {
        _super.prototype.initResponse.call(this, res);
        var groupID = req.params.id;
        this.docsService.getDocsByGroupId(groupID, function (err, docs) {
            res.send(JSON.stringify(docs));
        });
    };
    DocController.prototype.getDocByProjectId = function (req, res) {
        var _this = this;
        _super.prototype.initResponse.call(this, res);
        var projectId = req.params.id;
        this.projectsService.getProject(projectId, function (err, project) {
            _this.docsService.getDocsByProjectId(projectId, function (err, docs) {
                _this.groupsService.getGroups(projectId, function (err, groups) {
                    res.send(JSON.stringify(DocsWithProjectDto_1.DocsWithProjectDto.toReturn(project, groups, docs)));
                });
            });
        });
    };
    DocController.prototype.updateDoc = function (req, res) {
        var docId = req.params.id;
        this.docsService.udpateDoc(UpdateDocDto_1.UpdateDocDto.toDoc(docId, req.body), function (err, doc) {
            res.send(JSON.stringify(doc));
        });
    };
    DocController.prototype.deleteDoc = function (req, res) {
        var docId = req.params.id;
        this.docsService.deleteDoc(docId, function (err, doc) {
            res.send(JSON.stringify(doc));
        });
    };
    return DocController;
}(BaseController_1.BaseController));
exports.DocController = DocController;
